package io.renren.modules.job.task;

import io.renren.common.utils.DateUtils;
import io.renren.common.utils.RedisUtils;
import io.renren.common.utils.StrUtil;
import io.renren.modules.weixin.entity.BaseToken;
import io.renren.modules.weixin.util.WeiXinUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Struct;

/**
 * @Author: zhangxinlin
 * @Description:
 * @Date: Create in 11:29 2018/1/23
 * @Modified By：
 */
@Component("refreshTokenTask")
public class RefreshTokenTask {
    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private WeiXinUtils weiXinUtils;


    public void refreshToken(){
        logger.info("刷新微信token,JSAPITICKET,开始");
        try{
             String baseToken = weiXinUtils.getBaseTokenFromWX();
            if(StrUtil.isNotBlank(baseToken)){
                redisUtils.set(WeiXinUtils.WEIXIN.BASE_TOKEN_KEY,baseToken, DateUtils.Time.HOUR*2);
                redisUtils.set(WeiXinUtils.WEIXIN.JS_API_TICKET_KEY,weiXinUtils.getJSAPITicketFromWX(baseToken),DateUtils.Time.HOUR*2);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        logger.info("刷新微信token,JSAPITICKET,开始");
    }
}
